const mysql = require('mysql');
const pool = mysql.createPool({
  host: '127.0.0.1',
  port: '3306',
  user: 'root',
  password: '',
  database: 'hospital',
  connectionLimit: 15,
  multipleStatements: true
});

// 封装query
const query = (sql, val) => {
  return new Promise((resolve, reject) => {
    // 获取一个连接
    pool.getConnection((err, connection) => {
      if (err) {
        return reject(err);
      }
      // 执行SQL命令
      connection.query(sql, val, (err, result) => {
        if(err) {
          return reject(err)
        }
        // 获取成功结果
        resolve(result)
      });
      // 释放这个连接
      connection.release()
    });

    // 释放这个连接
  });
};

module.exports = {
  query
}